<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\View;
use think\facade\Request;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Session;
use app\common\model\User;
use app\common\model\Taocan;
use app\common\model\UserGroup;
use app\common\model\UserAuthRule;
use app\common\model\Shop as ShopModel;

class Shop extends AdminBase
{
    public function initialize()
    {
        parent::initialize();
        $this->taocan = Taocan::lists();
    }
    //商户列表
    public function index()
    {
        return $this->fetch('index');
    }
    //商户列表
    public function index_json($limit)
    {
        $list = ShopModel::with(['taocan'])->order('id desc')->paginate($limit);
        $this->result($list);
    }
    //添加
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $verify = input('_verify', true);
            try{
                $this->validate($param, 'shop');
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }
            empty($param['password']) && $this->error('密码不能为空');
            $result = ShopModel::create($param);
            if ($result == true) {
                //添加角色组
                $authgroup['shop_id'] = $result->id;
                $authgroup['name'] = "管理员";
                $authgroup['status'] = "1";
                //获取所有权限
                $authgroup['rules'] = Taocan::where('id',$param['taocan'])->value('rules');
                $aug = UserGroup::create($authgroup);
                //添加管理员
                $adduser['shop_id'] = $authgroup['shop_id'];
                $groupid = $aug->id;
                $adduser['name'] = $param['name'];
                $adduser['mobile'] = $param['mobile'];
                $adduser['password'] = $param['password'];
                $adduser['status'] = "1";
                $adduser['group_id'] = $groupid;
                $user = User::create($adduser);
                //更新管理员
                ShopModel::where('id',$result->id)->update(['admin_id'=>$user->id]);
                $this->success('操作成功', url('admin/shop/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('add',['taocan'=> $this->taocan]);
    }
    //编辑
    public function edit()
    {
        $id = input('id') ?: '参数错误';
        $data = ShopModel::where('id',$id)->find();
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'shop');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $resule = ShopModel::update($param,['id'=>$param['id']]);
            if ($resule == true) {
                insert_admin_log('修改了商家');
                $this->success('修改成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save',[
            'data' => $data,
            'taocan'=> $this->taocan
        ]);
    }
    //删除
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            ShopModel::destroy($param['id']);
            insert_admin_log('删除了商家');
            $this->success('删除成功');
        }
    }
    //商户列表
    public function taocan()
    {
        return $this->fetch('taocan');
    }
    //商户列表
    public function taocan_json($limit)
    {
        $list = Taocan::order('id desc')->paginate($limit);
        $this->result($list);
    }
    //添加
    public function add_taocan()
    {
        if ($this->request->isPost()) {
            $result = Taocan::create($this->request->param());
            if ($result == true) {
                insert_admin_log('添加了用户组');
                $this->success('添加成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        $userAuthRule = collection(UserAuthRule::where(['status' => 1])->order('sort_order asc')->select())->toArray();
        foreach ($userAuthRule as $k => $v) {
            //$authRule[$k]['open'] = true;
        }
        return View::fetch('save_taocan', ['userAuthRule' => list_to_tree($userAuthRule)]);
    }
    //编辑
    public function edit_taocan()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $verify = input('_verify', true);
            //验证规则
            if($verify!='0'){
                try{
                    $this->validate($param, 'UserGroup');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            //更新数据
            $resule = Taocan::update($param,['id'=>$param['id']]);
            if ( $resule == true) {
                insert_admin_log('修改了套餐');
                $this->success('修改成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        $data     = Taocan::where('id', input('id'))->find();
        $userAuthRule = collection(UserAuthRule::where(['status' => 1])->order('sort_order asc')->select())->toArray();
        foreach ($userAuthRule as $k => $v) {
            // $authRule[$k]['open'] = true;
            $userAuthRule[$k]['checked'] = in_array($v['id'], explode(',', $data['rules']));
        }
        return $this->fetch('save_taocan', ['data' => $data, 'userAuthRule' => list_to_tree($userAuthRule)]);
    }
    //删除
    public function del_taocan()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Taocan::destroy($param['id']);
            insert_admin_log('删除了套餐');
            $this->success('删除成功');
        }
    }
    
}
